location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
WHERE := $(location)
$(info ** -> $(WHERE))
$(info ** ------------------------------------------------------------------ **)

SHELL := /bin/bash # force avoidance of dash as shell
thepwd = "$(shell pwd)"

default: all

pylint:
	$$SHELL test.sh

pyformat:
	@status=0; \
	for py in $$(find h2o4gpu -name "*.py" -type f); do \
		if [ "$$py" == "h2o4gpu/__init__.py" ]; \
		then echo "Skip $$py"; \
		else echo $$py; \
		yapf -p -i --style google $$py; \
		fi; \
	done; \

prep:
	PYVER=`python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))' | sed 's/\.//g'` && sed -i 's/python-tag=.*/python-tag=py'$$PYVER'/g' setup.cfg
	PYVER=`python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))'` && sed -i 's/python_version==.*/python_version=='$$PYVER'/g' setup.cfg
	cd ../../ && bash scripts/apply_sklearn_initmerge.sh # so if change our init it gets updated

all: prep

	# for incorporating xgboost and py3nvml builds
	ln -sf ../../xgboost/python-package/xgboost .
	ln -sf ../../py3nvml/py3nvml .

	python setup.py sdist bdist_wheel

	# update build with xgboost shared library
	mkdir -p build/lib/xgboost/
	cd build/lib/xgboost/ ; ln -sf ../../../../../xgboost/python-package/xgboost/libxgboost.so . ; cd ../../../

	# Make wheel with other builds added
	rm -rf dist/*.whl
	python setup.py sdist bdist_wheel

	# for pycharm
	ln -sf $(thepwd)/../interface_c/_ch2o4gpu_cpu.so .
	ln -sf $(thepwd)/../interface_c/_ch2o4gpu_gpu.so .


install:
	-cd ../.. && pip uninstall -y h2o4gpu
	-cd ../.. && pip uninstall -y xgboost
	-cd ../.. && pip uninstall -y py3nvml
	find -name *.pyc | xargs rm -rf
	find -name *__pycache__ | xargs rm -rf
	# something wrong with below
	#python setup.py install
	pip install dist/h2o4gpu*.whl --upgrade

clean:
	rm -f h2o4gpu/BUILD_INFO.txt
	rm -f h2o4gpu/dist*
    # A bit hacky b/c of how we are merging __init__.py with other frameworks
    # This *is* required
	rm -f h2o4gpu/__init__.py
	touch h2o4gpu/__init__.py
	pip install numpy
	python setup.py clean --all && rm -rf h2o4gpu.egg-info && rm -rf h2o4gpu/__pycache__/ && rm -rf dist/
	cd h2o4gpu && find -L -name *.pyc | xargs rm -rf


